package cn.zjyj.module.zjyj.controller.admin.deviceinstance;

import cn.zjyj.module.zjyj.dal.bo.deviceinstance.DeviceInstanceBo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.zjyj.framework.common.common.pojo.PageParam;
import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.framework.excel.core.util.ExcelUtils;

import cn.zjyj.framework.apilog.core.annotation.ApiAccessLog;
import static cn.zjyj.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.zjyj.module.zjyj.controller.admin.deviceinstance.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.deviceinstance.DeviceInstanceDO;
import cn.zjyj.module.zjyj.service.deviceinstance.DeviceInstanceService;

@Tag(name = "管理后台 - 设备实例")
@RestController
@RequestMapping("/zjyj/device-instance")
@Validated
public class DeviceInstanceController {

    @Resource
    private DeviceInstanceService deviceInstanceService;

    @PostMapping("/create")
    @Operation(summary = "创建设备实例")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance:create')")
    public CommonResult<Long> createDeviceInstance(@Valid @RequestBody DeviceInstanceSaveReqVO createReqVO) {
        return success(deviceInstanceService.createDeviceInstance(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备实例")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance:update')")
    public CommonResult<Boolean> updateDeviceInstance(@Valid @RequestBody DeviceInstanceSaveReqVO updateReqVO) {
        deviceInstanceService.updateDeviceInstance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备实例")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance:delete')")
    public CommonResult<Boolean> deleteDeviceInstance(@RequestParam("id") Long id) {
        deviceInstanceService.deleteDeviceInstance(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备实例")
                @PreAuthorize("@ss.hasPermission('zjyj:device-instance:delete')")
    public CommonResult<Boolean> deleteDeviceInstanceList(@RequestParam("ids") List<Long> ids) {
        deviceInstanceService.deleteDeviceInstanceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备实例")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance:query')")
    public CommonResult<DeviceInstanceRespVO> getDeviceInstance(@RequestParam("id") Long id) {
        DeviceInstanceDO deviceInstance = deviceInstanceService.getDeviceInstance(id);
        return success(BeanUtils.toBean(deviceInstance, DeviceInstanceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备实例分页")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance:query')")
    public CommonResult<PageResult<DeviceInstanceRespVO>> getDeviceInstancePage(@Valid DeviceInstancePageReqVO pageReqVO) {
        PageResult<DeviceInstanceBo> deviceInstancePage = deviceInstanceService.getDeviceInstancePage(pageReqVO);
        return success(BeanUtils.toBean(deviceInstancePage, DeviceInstanceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备实例 Excel")
    @PreAuthorize("@ss.hasPermission('zjyj:device-instance:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceInstanceExcel(@Valid DeviceInstancePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceInstanceBo> list = deviceInstanceService.getDeviceInstancePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备实例.xls", "数据", DeviceInstanceRespVO.class,
                        BeanUtils.toBean(list, DeviceInstanceRespVO.class));
    }

}